﻿#region  文件信息
// ---------------------------------------------------------------------------------------
// <copyright file="ExFilterAttribute.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：ExFilterAttribute.cs
// 创建时间：2015-03-11 9:43
// 修改时间：2015-03-11 9:43
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using System;
using System.Web.Mvc;
using AmazeUI.bll;
using Ema.Controllers;
using Ema.Models;
using Ema.Models.XtModel;

namespace Ema.Filters
{
    /// <summary>
    /// 异常处理
    /// </summary>
    public class ExFilterAttribute : IExceptionFilter
    {
        /// <summary>
        /// 异常处理
        /// </summary>
        /// <param name="filterContext"></param>
        public void OnException(ExceptionContext filterContext)
        {
            Exception exception = filterContext.Exception;
            filterContext.ExceptionHandled = true;
            var controllers = filterContext.Controller.ControllerContext.RouteData.Values["controller"].ToString();
            var action = filterContext.Controller.ControllerContext.RouteData.Values["action"].ToString();
            string url = string.Empty;
            try
            {
                url = filterContext.Controller.ControllerContext.RequestContext.HttpContext.Request.RawUrl;
            }
            catch
            {
            }
            Chuli(exception, controllers, action, url);

            filterContext.Result = new ErrorController().Msg(exception, controllers, action, IsJson);
        }

        /// <summary>
        /// 处理错误
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="controllers"></param>
        /// <param name="action"></param>
        /// <param name="url"></param>
        private static void Chuli(Exception exception, string controllers, string action, string url)
        {
            var erb = new XtErrorBll();
            var xtError = new XtError();
            xtError.Action = action;
            xtError.Controllers = controllers;
            xtError.Url = url;
            xtError.Name = exception.Message;
            xtError.Userid = XtCom.LoginModel.Id;
            erb.AddError(xtError);

        }

        /// <summary>
        /// Gets or sets a value indicating whether is json.
        /// </summary>
        public bool IsJson { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ExFilterAttribute"/> class.
        /// </summary>
        public ExFilterAttribute()
        {
            this.IsJson = false;
        }


    }


}